package com.dongdongshop.service.impl.specification;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specification_option.TbSpecificationOption;
import com.dongdongshop.pojo.specification_option.TbSpecificationOptionExample;
import com.dongdongshop.service.specification.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    //查询规格
    @Override
    public List<TbSpecification> listSpecification(String specName) {
        List<TbSpecification> list = tbSpecificationMapper.listSpecification(specName);
        return list;
    }
    //添加规格
    @Override
    @Transactional
    public int insertSpecification(List<TbSpecificationOption> tbSpecificationOptions, TbSpecification specification) {
        //添加规格并返回id
        int specSuccNum = tbSpecificationMapper.insertSpecificationByspecName(specification);
        //批量添加规格选项
        int OprionSuccNum = tbSpecificationOptionMapper.insertBatch(specification.getId(),tbSpecificationOptions);
        return OprionSuccNum;
    }

    @Override
    public TbSpecification getSpecification(Long id) {
        return tbSpecificationMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateSpecification(List<TbSpecificationOption> tbSpecificationOptions, TbSpecification tbSpecification) {
        //修改规格
        int specSuccNum = tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //中间表获取 规格id对应的规格选项id
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        //批量删除规格选项
        tbSpecificationOptionMapper.deleteByExample(example);
        //批量添加规格选项
        int OprionSuccNum = tbSpecificationOptionMapper.insertBatch(tbSpecification.getId(), tbSpecificationOptions);
        return OprionSuccNum;
    }

    @Override
    @Transactional
    public int deleteSpecificationBatch(Long[] ids) {
        //删除规格
        int specSuccNum = tbSpecificationMapper.deleteSpecificationBatch(ids);
        //批量删除规格
        for (Long id : ids) {
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(example);
        }
        return specSuccNum;
    }

    @Override
    public List<TbSpecification> listSpecificationEcho() {
        List<TbSpecification> list = tbSpecificationMapper.selectByExample(null);
        return list;
    }
}
